﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using FlickrNet;

namespace TagPad
{
	public partial class MainForm : Form
	{
		private string sTempFrob_;
		private string sApiKey_ = "ffc0aac543f3227129e83b231e9d24a8";
		private string sSharedSecret_ = "c1e7895abb8a41e9";
		private Flickr flickr_;

		public MainForm()
		{
			InitializeComponent();
			flickr_ = new Flickr(sApiKey_, sSharedSecret_);
		}

		private void authenticateButton_Click(object sender, EventArgs e)
		{
			sTempFrob_ = flickr_.AuthGetFrob();
			try {
				flickr_.AuthToken = flickr_.AuthGetToken(sTempFrob_).Token;
				doneButton.Enabled = false;
			}
			catch(FlickrException)
			{
				System.Diagnostics.Process.Start(flickr_.AuthCalcUrl(sTempFrob_, AuthLevel.Write));
			}
		}

		private void doneButton_Click(object sender, EventArgs e)
		{
			try {
				flickr_.AuthToken = flickr_.AuthGetToken(sTempFrob_).Token;
				doneButton.Enabled = false;
			}
			catch(FlickrException)
			{
				MessageBox.Show("Authentication failed!");
			}

	//        OutputTextBox.AppendText("Retrieving photosets..." + Environment.NewLine);
	//        PhotosetsObj = FlickrObj.PhotosetsGetList();
	//        Photoset[] PhotosetArray = PhotosetsObj.PhotosetCollection;
	//        foreach (Photoset Ps in PhotosetArray)
	//            SetsListBox.Items.Add(Ps.Title);

		}

		private void setTextBox_KeyPress(object sender, KeyPressEventArgs e)
		{
			if (e.KeyChar != '\r')
				return;
			
		}

	}

	//public partial class Form1 : Form
	//{
	//    private string sTempFrob;
	//    private string sApiKey = "cb08047f4e797d6c336c7f27b1a599a4";
	//    private string sSharedSecret = "f540c10fb8d7b932";
	//    private string sAuthentication = "Authentication: ";
	//    private Flickr FlickrObj;
	//    private bool bUiChange = true;

	//    private Photosets PhotosetsObj = null;
	//    private int iCurrentSet = 0;
	//    private PhotoCollection SetPhotos = new PhotoCollection();
	//    private int iCurrentPhoto = 0;
	//    private bool bImageLoading = false;

	//    private delegate void ActionDelegate();
	//    private List<Action> Actions;

	//    public Form1()
	//    {
	//        InitializeComponent();
	//        AuthenticationStatusLabel.Text = sAuthentication + "Failure!";
	//        SetCurrentPhoto(-1);
	//        FlickrObj = new Flickr(sApiKey, sSharedSecret);
	//    }

	//    private void UpdateControlValues_()
	//    {
	//        PhotoPermissions Perms = !bImageLoading && iCurrentPhoto >= 0 && iCurrentPhoto < SetPhotos.Count
	//            ? FlickrObj.PhotosGetPerms(SetPhotos[iCurrentPhoto].PhotoId) : null;

	//        PrivateRadioButton.Checked = Perms != null ? !Perms.IsPublic : false;
	//        FriendsCheckBox.CheckState = Perms != null && Perms.IsFriend ? CheckState.Checked : CheckState.Unchecked;
	//        FamilyCheckBox.CheckState = Perms != null && Perms.IsFamily ? CheckState.Checked : CheckState.Unchecked;
	//        PublicRadioButton.Checked = Perms != null ? Perms.IsPublic : false;

	//        EnableControls_();
	//    }
		
	//    private void EnableControls_()
	//    {

	//        bool bEnableControls = !bImageLoading && iCurrentPhoto >= 0 && iCurrentPhoto < SetPhotos.Count;

	//        PrivateRadioButton.Enabled = bEnableControls;
	//        FriendsCheckBox.Enabled = bEnableControls && PrivateRadioButton.Checked;
	//        FamilyCheckBox.Enabled = bEnableControls && PrivateRadioButton.Checked;
	//        PublicRadioButton.Enabled = bEnableControls;

	//        QueueActionsCheckBox.Enabled = ActionsListBox.Items.Count == 0;
	//        ApplyButton.Enabled = ActionsListBox.Items.Count != 0; // || SingleAction != null;
	//    }

	//    private void AuthenticateButton_Click(object sender, EventArgs e)
	//    {
	//        //Flickr FlickrObj = new Flickr(sApiKey, sSharedSecret);
	//        sTempFrob = FlickrObj.AuthGetFrob();
	//        string sAuthenticationUrl = FlickrObj.AuthCalcUrl(sTempFrob, AuthLevel.Write);
	//        System.Diagnostics.Process.Start(sAuthenticationUrl);
	//    }

	//    private void AuthenticatedButton_Click(object sender, EventArgs e)
	//    {
	//        OutputTextBox.AppendText("Attempting authentication check..." + Environment.NewLine);
	//        //Flickr FlickrObj = new Flickr(sApiKey, sSharedSecret);
	//        try {
	//            Auth AuthObj = FlickrObj.AuthGetToken(sTempFrob);
	//            OutputTextBox.AppendText("User authenticated successfully" + Environment.NewLine);
	//            OutputTextBox.AppendText("Authentication token is " + AuthObj.Token + Environment.NewLine);
	//            FlickrObj.AuthToken = AuthObj.Token;
	//            OutputTextBox.AppendText("User id is " + AuthObj.User.UserId + Environment.NewLine);
	//            AuthenticationStatusLabel.Text = sAuthentication + "Success";
	//        }
	//        catch(FlickrException ex)
	//        {
	//            OutputTextBox.AppendText("User did not authenticate you" + Environment.NewLine);
	//            OutputTextBox.AppendText(ex.ToString());
	//            AuthenticationStatusLabel.Text = sAuthentication + "Failure!";
	//        }

	//        OutputTextBox.AppendText("Retrieving photosets..." + Environment.NewLine);
	//        PhotosetsObj = FlickrObj.PhotosetsGetList();
	//        Photoset[] PhotosetArray = PhotosetsObj.PhotosetCollection;
	//        foreach (Photoset Ps in PhotosetArray)
	//            SetsListBox.Items.Add(Ps.Title);
	//    }

	//    private void SetsListBox_SelectedIndexChanged(object sender, EventArgs e)
	//    {
	//        SetPhotos.Clear();
	//        iCurrentSet = SetsListBox.SelectedIndex;
	//        //uint uPhotoCount = PhotosetsObj.PhotosetCollection[uCurrentSet].NumberOfPhotos;
	//        //uint uPages = Ceiling uPhotoCount / 100.0
	//        //for (uint 
	//        Photoset CurrentPhotoset = FlickrObj.PhotosetsGetPhotos(PhotosetsObj.PhotosetCollection[iCurrentSet].PhotosetId);
	//        SetPhotos.AddRange(CurrentPhotoset.PhotoCollection);
	//        SetCurrentPhoto(0);
	//    }

	//    private void NextButton_Click(object sender, EventArgs e)
	//        { SetCurrentPhoto(iCurrentPhoto + 1); }

	//    private void PreviousButton_Click(object sender, EventArgs e)
	//        { SetCurrentPhoto(iCurrentPhoto - 1); }

	//    private void SetCurrentPhoto(int iNewCurrentPhoto)
	//    {
	//        iCurrentPhoto = Math.Min(Math.Max(0, iNewCurrentPhoto), SetPhotos.Count - 1); // Sets -1 if no photos.

	//        PreviousButton.Enabled = iCurrentPhoto > 0;
	//        NextButton.Enabled = iCurrentPhoto >= 0 && iCurrentPhoto < SetPhotos.Count - 1;
	//        CurrentPhotoLabel.Text = "Curr:" + (iCurrentPhoto < 0 ? "" : " " + Convert.ToString(iCurrentPhoto + 1));
	//        TotalPhotosLabel.Text = "Total: " + SetPhotos.Count;

	//        bool bValidCurrentPhoto = iCurrentPhoto >= 0;
	//        if (bValidCurrentPhoto) {
	//            bImageLoading = true;
	//            PictureBox.ImageLocation = SetPhotos[iCurrentPhoto].MediumUrl;
	//        }
	//        UpdateControlValues_(); // Calls EnableControls_ as well.
	//    }

	//    private void PictureBox_LoadCompleted(object sender, AsyncCompletedEventArgs e)
	//    {
	//        bImageLoading = false;
	//        bUiChange = false;
	//        UpdateControlValues_(); // Calls EnableControls_ as well.
	//    }

	//    private void PrivateRadioButton_CheckedChanged(object sender, EventArgs e)
	//    {
	//        if (bUiChange)
	//            RecordUserModification_();
	//        EnableControls_();
	//    }

	//    private void PublicRadioButton_CheckedChanged(object sender, EventArgs e)
	//    {
	//        if (bUiChange)
	//            RecordUserModification_();
	//        EnableControls_();
	//    }

	//    private void RecordUserModification_()
	//    {
	//        Photo PhotoObj = iCurrentPhoto >= 0 && iCurrentPhoto < SetPhotos.Count ? SetPhotos[iCurrentPhoto] : null;
	//        if (PhotoObj == null)
	//            return;
	//        Actions.Add(new PermChange(PhotoObj, PublicRadioButton.Checked, FriendsCheckBox.Checked, FamilyCheckBox.Checked));
	//        ActionsListBox.Items.Add("PermChange");
						
	//        //if (QueueActionsCheckBox.Checked) {
	//        //    // Record action in list box.
	//        //}
	//        //else {
	//        //    // Record action singly.
	//        //}
			
	//    }

	//    private void ApplyButton_Click(object sender, EventArgs e)
	//    {
	//        //if (ActionsListBox.Items.Count == 0) {
	//        //    // Do single current action.
	//        //}
	//        //else {
	//        //    // Process all actions in list box.
	//        //}
			
	//    }

	//    private void QueueActionsCheckBox_CheckedChanged(object sender, EventArgs e)
	//    {
	//        // If single action, move it to the list box.
	//    }
	//}

}
